<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button
        :isChecked="isSelectAll"
        class="check-button"
        @click.native="checkClick"
      ></check-button>
      <span>全选</span>
    </div>

    <div class="totalprice">合计：{{ totalPrice }}</div>

    <div class="calculate" @click="calcClick">
      去计算：({{ checkedLength }})
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

import CheckButton from "components/content/checkButton/CheckButton";

export default {
  data() {
    return {
      checkedPrice: 0,
      total: 0,
    };
  },
  components: {
    CheckButton,
  },
  // updated(){
  //   console.log(this.total);
  //   console.log(this.checkedPrice);
  // },
  computed: {
    ...mapGetters(["cartList"]),
    totalPrice() {
      return (
        "￥" +
        this.$store.state.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },

    checkedLength() {
      // return this.$store.getters.checkedLength
      return this.$store.state.cartList.filter((item) => item.checked).length;
    },

    isSelectAll() {
      // if (this.cartList.length == 0) return false;
      for (let item of this.cartList) {
        if (!item.checked) {
          return false;
        }
      }
      return true;
      // return !this.cartList.find((item) => !item.checked);
    },
  },

  methods: {
    checkClick() {
      if (this.isSelectAll) {
        //全部选中
        this.cartList.forEach((item) => {
          item.checked = false;
        });
      } else {
        //部分选中或全都没选中
        this.cartList.forEach((item) => {
          item.checked = true;
        });
      }
    },
    calcClick() {
      if (this.cartList.length == 0) {
        this.$toast.show("请选择购买的商品", 2000);
      }
    },
  },
};
</script>

<style scoped>
.bottom-bar {
  display: flex;
  height: 44px;
  background-color: #eee;
  position: relative;
  bottom: 44px;
  line-height: 44px;
  align-items: center;
  /* justify-content:space-between */
}
.check-content {
  display: flex;
  align-items: center;
  flex: 1;
}
.check-button {
  margin-right: 5px;
  width: 20px;
  height: 20px;
  line-height: 20px;
}
.totalprice {
  flex: 1;
}
.calculate {
  text-align: center;
  background-color: red;
  color: #fff;
  flex: 1;
}
</style>